var Map = require("utils/Map.js");
var Global = require("Global.js");
var Api = require("Api.js");

//加入购物车
function setCartStorage(Cart, date, wday) {
    var CartList = wx.getStorageSync('Cart');
    var curId = Cart.id;
    var flag = true;
    for (var i = 0; i < CartList.length; i++) {
        var item = CartList[i];
        var id = item.productId;
        var itemDate = item.date;
        //购物车已有该商品，更新
        if (id == curId && date == itemDate) {
            flag = false;
            var num = parseInt(item.num) + 1;
            var totalPrice = parseFloat(item.price) * num;
            item.num = num;
            item.totalPrice = totalPrice;
            CartList[i] = item;
            break;
        }
    }
    //购物车没有该商品，增加
    if (flag) {
        if (CartList == null || CartList.length == 0) {
            CartList = new Array();
        }
        var num = 1;
        var param = {
            image: solveImg(Cart.image),
            num: num,
            name: Cart.name,
            categoryId: Cart.categoryId,
            date: date,
            week: solveDate(wday),
            price: Global.toDecimal2(Cart.price),
            totalPrice: Cart.price * num,
            productId: Cart.id,
            productClassId: Cart.productClassId
        };
        CartList.push(param);
    }
    wx.setStorageSync('Cart', CartList);
}

//图片处理
function solveImg(img) {
    if(img.indexOf(Api.getHostUrl()) < 0) {
        img = "http://" + Api.getHostUrl() + img;
    }
    return img;
}

//获取购物车数据
function geCartStorage() {
    return wx.getStorageSync('Cart');
}

//清空购物车数据
function clearCartStorage() {
    wx.setStorageSync('Cart', "");
}

//计算购物车商品数量
function countCartNum() {
    var CartList = wx.getStorageSync('Cart');

    var num = 0;
    if (CartList != null && CartList.lenght != 0) {
        for (var i = 0; i < CartList.length; i++) {
            num += CartList[i].num;
        }
    }
    return num;
}

//计算购物车商品总价
function countCartPrice() {
    var CartList = wx.getStorageSync('Cart');
    var totalPrice = 0;
    if (CartList != null && CartList.lenght != 0) {
        for (var i = 0; i < CartList.length; i++) {
            totalPrice += (parseFloat(CartList[i].price) *  CartList[i].num);
        }
    }
    return Global.toDecimal2(totalPrice);
}

//购物车更新 
function updateCartNum(curPrduct, type) {
    var curProductId = curPrduct.productId;
    var date = curPrduct.date;
    var CartList = wx.getStorageSync('Cart');
    var goalProduct;
    var goalIndex;
    for (var i = 0; i < CartList.length; i++) {
        var item = CartList[i];
        if (item.productId == curProductId && date == item.date) {
            goalProduct = item;
            goalIndex = i;
            break;
        }
    }
    if (type == 1) { //增加
        var num = goalProduct.num;
        num++;
        goalProduct.num = num;
        goalProduct.totalPrice = num * goalProduct.price;
        CartList[goalIndex] = goalProduct;
    } else if (type == 2) { //减
        var num = goalProduct.num;
        num--;
        if (num == 0) {
            CartList.splice(goalIndex, 1);
        } else {
            goalProduct.num = num;
            goalProduct.totalPrice = num * parseFloat(goalProduct.price);
            CartList[goalIndex] = goalProduct;
        }
    }
    wx.setStorageSync('Cart', CartList);
}

//更新购物车送达日期 
function updateDate(curPrduct, curDate, index) {
    var curProductId = curPrduct.productId;
    var date = curPrduct.date;
    var CartList = wx.getStorageSync('Cart');
    var goalProduct;
    var goalIndex;
    for (var i = 0; i < CartList.length; i++) {
        var item = CartList[i];
        if (item.productId == curProductId && date == item.date) {
            goalProduct = item;
            goalIndex = i;
            break;
        }
    }
    goalProduct.date = curDate.wdate;
    goalProduct.week = curDate.week;
    goalProduct.activeIndex = index;
    CartList[goalIndex] = goalProduct;
    wx.setStorageSync('Cart', CartList);
}

//进入购物车时更新每个子项的送达日期的有效下标标
function updateActiveIndex(allDate) {
    var CartList = wx.getStorageSync('Cart');
    for (var i = 0; i < CartList.length; i++) {
        var item = CartList[i];
        var curDate = item.date;
        var activeIndex = 0;
        var flag = true;
        for (var k = 0; k < allDate.length; k++) {
            var curItem = allDate[k];
            if (curItem.wdate == curDate) {
                activeIndex = k;
                flag = false;
                break;
            }
        }
        if (flag) {
            CartList.splice(i, 1);
            i--;
        } else {
            CartList[i].activeIndex = activeIndex;
        }
    }
    wx.setStorageSync('Cart', CartList);
}

function setAddressStorage(address) {
    wx.setStorageSync('address', address);
}

function getAddressStorage() {
    return wx.getStorageSync('address');
}

function setCouponStorage(coupon) {
    wx.setStorageSync('coupon', coupon);
}

function getCouponStorage() {
    return wx.getStorageSync('coupon');
}

function getOrderList() {
    var CartList = wx.getStorageSync('Cart');
    var map = new Map();

    var vis = new Array(CartList.length);
    for (var i = 0; i < vis.length; i++) {
        vis[i] = false;
    }
    for (var i = 0; i < CartList.length; i++) {
        if (vis[i] == false) {
            var cur = CartList[i];
            var curCategoryId = cur.categoryId;
            var curProductId = cur.productId;
            var curDate = cur.date;
            var curMapList = new Array();
            curMapList.push(cur);
            for (var k = i + 1; k < CartList.length; k++) {
                if (vis[k] == false) {
                    var item = CartList[k];
                    if (item.productId == curProductId && curDate == item.date) {
                        curMapList.push(item);
                        vis[k] = true;
                    }
                }
            }
            var mapList = solveMapData(curMapList);
            var mapKey = cur.productId + "," + cur.date;
            map.put(mapKey, mapList);
            vis[i] = true;
        }
    }
    return map.toString();
}

function solveMapData(list) {
    var newMapList = new Array();
    for (var i = 0; i < list.length; i++) {
        var item = list[i];
        var newItem = {
            "categoryId": item.categoryId,
            "date": item.date,
            "goodAccount": item.num,
            "goodId": item.productId,
            "goodImage": item.image,
            "goodName": item.name,
            "productClassId": item.productClassId
        };
        newMapList.push(newItem);
    }
    return newMapList;
}

//处理商品数据
function solveProductData(list, curDate) {
    var newList = new Array();
    if(!list) {
        return newList;
    }
    var CartList = wx.getStorageSync('Cart');
    for (var i = 0; i < list.length; i++) {
        var product = list[i];
        var flag = false;
        for(var k = 0; k < CartList.length; k ++) {
            var cart = CartList[k];
            if(product.id == cart.productId && curDate == cart.date) {
                flag = true;
                product.isShowCartNum = true;
                product.cartNum = cart.num;
                newList.push(product);
                break;
            }
        }
        if(!flag) {
            product.isShowCartNum = false;
            newList.push(product);
        }
    }
    return newList;
}

//非购物车页面，购物减减
function subCartNum(product, curDate) {
    var CartList = wx.getStorageSync('Cart');
    for(var i = 0; i < CartList.length; i ++) {
        var cart = CartList[i];
        if(cart.productId == product.id && cart.date == curDate) {
            if (cart.num > 0) {
                cart.num = cart.num - 1;
                cart.totalPrice = cart.num * parseFloat(cart.price);
                CartList[i] = cart;
                if(cart.num == 0) {
                     CartList.splice(i, 1);
                }
            } else {
                 CartList.splice(i, 1);
            }
            break;
        }
    }
    wx.setStorageSync('Cart', CartList);
}

function solveDate(date) {
    var week = "周日";
    date = parseInt(date);
    switch(date) {
        case 2:
            week = "周一";
            break;
        case 3:
            week = "周二";
            break;
        case 4:
            week = "周三";
            break;
        case 5:
            week = "周四";
            break;
        case 6:
            week = "周五";
            break;
        case 7:
            week = "周六";
            break;
    }
    return week;
}

module.exports = {
    setCartStorage: setCartStorage,
    geCartStorage: geCartStorage,
    clearCartStorage: clearCartStorage,
    countCartNum: countCartNum,
    countCartPrice: countCartPrice,
    updateCartNum: updateCartNum,
    updateDate: updateDate,
    updateActiveIndex: updateActiveIndex,
    getAddressStorage: getAddressStorage,
    setAddressStorage: setAddressStorage,
    setCouponStorage: setCouponStorage,
    getCouponStorage: getCouponStorage,
    getOrderList: getOrderList,
    solveProductData: solveProductData,
    subCartNum: subCartNum,
    solveDate: solveDate
}